
import { saveCookie, getCookie } from '@/service/util.service.js'
var userdata = {
    data() {
        return {
            mobile:this.$route.query.mobile,
            timeEndNum: '',
            userConfig:{},
        }
    },
    created(){
        this.getUser()
    },
    mounted() {
        // this.login()
        // this.getUserData()
    },
    methods: {
        getUser(){
            let user = getCookie('user') && JSON.parse(getCookie('user')) || {}
            user.guide = (this.$route.query.id || this.$route.query.id) ? (this.$route.query.id || this.$route.query.id) : user.guide;
            this.$store.state.auth.user = user

            if(this.$route.query.mobile){
                this.mobile = this.$route.query.mobile;
                sessionStorage.setItem('mobile', this.mobile);
            }
            let base_access_token = '';
            if(this.$route.query.access_token || this.$route.query.base_access_token){
                sessionStorage.setItem('base_access_token',`${this.$route.query.base_access_token || this.$route.query.access_token}`)
                user.access_token = (this.$route.query.access_token || this.$route.query.base_access_token) ? (this.$route.query.access_token || this.$route.query.base_access_token) : user.access_token;
                user.session_id = (this.$route.query.access_token || this.$route.query.base_access_token) ? (this.$route.query.access_token || this.$route.query.base_access_token) : user.access_token;
                user.base_udid = (this.$route.query.udid || this.$route.query.base_udid) ? (this.$route.query.udid || this.$route.query.base_udid) : user.base_udid;
            }else{
                base_access_token = sessionStorage.getItem('base_access_token')
                user.access_token = user.access_token || base_access_token;
                user.session_id = user.session_id || base_access_token;
                user.base_udid = user.base_udid || base_access_token;
            }
            this.userConfig = user
            saveCookie('user', user)
            this.$store.state.auth.base_app_id = user.base_app_id
            this.$store.state.auth.base_project_id = user.base_project_id
            this.$store.state.auth.guide = user.guide
            this.$store.state.auth.app_id = user.base_app_id
            this.$store.state.auth.project_id = user.base_project_id
            this.$store.state.auth.guide = user.guide

            saveCookie('base_version', user.base_version)
            saveCookie('app_id', user.base_app_id)
            saveCookie('project_id', user.base_project_id)
            saveCookie('guide', user.guide)
            saveCookie('access_token', user.access_token || base_access_token)
        },
        login() {
            this.$http.post(`/auth/code-login`, {
                mobile: sessionStorage.getItem('mobile') || this.mobile,
                sms_code: sessionStorage.getItem('mobile') ? sessionStorage.getItem('mobile').slice(3, 9) : this.mobile.slice(3, 9),
            }).then(({ data }) => {
                this.$vux.loading.hide();
                if (data.status === 1) {
                    // this.$vux.toast.text(data.info, 'middle');
                    const user = data.data;
                    this.userConfig = user
                    
                    user.base_app_id = '145'//(this.$route.query.app_id || this.$route.query.base_app_id) ? (this.$route.query.app_id || this.$route.query.base_app_id) : user.base_app_id;
                    user.base_project_id = '126'//(this.$route.query.project_id || this.$route.query.base_project_id) ? (this.$route.query.project_id || this.$route.query.base_project_id) : user.base_project_id;
                    user.base_version = `4.2.00`
                    new Promise((resolve, reject) => {
                        // 保存登录信息到cookie
                        saveCookie('user', user);
                        resolve();
                    }).then(() => {
                        // 获取/刷新登录信息
                        this.$store.dispatch('getUserInfo');
                        this.$store.dispatch('loginSuccess', user);
                        
                        this.getUserData()
                    });
                } else {
                    this.$vux.toast.text(data.info, 'middle')
                }
            }).catch(err => {
                this.$vux.loading.hide();
                this.$vux.toast.text('网络状态不稳定，请稍后重试！', 'middle');
            });
        },
        getUserData() {
            this.$http.post(`/user/view`, {
                ...this.$route.query,
                base_front:1,
                base_access_token:this.userConfig.access_token,
                base_session_id:this.userConfig.session_id,
                base_udid:this.userConfig.base_udid,
                base_app_id:this.userConfig.base_app_id,
                base_project_id:this.userConfig.base_project_id,
                base_version:this.userConfig.base_version,
            }).then(({ data }) => {
                if (data.status == 1) {
                    saveCookie('timeEnd', data.data.article_vip_expire_time == 0)
                    saveCookie('vip', data.data.article_is_vip == 1)
                    this.vip = (data.data.article_is_vip == 1) ? 'true' : 'false'
                    this.timeEnd = (data.data.article_vip_expire_time == 0) ? 'true' : 'false'
                    this.mobile = data.data.user_format
                    this.timeEndNum = data.data.article_vip_format_expire_time
                }else{
                    saveCookie('vip', false)
                    this.vip = 'false'
                    this.timeEnd = 'false'
                }
            })
        },
    },
    beforeDestroy() {

    },
}
export default userdata